package cn.cxy.camerademo

import android.Manifest
import android.content.ContentValues
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.qw.soul.permission.SoulPermission
import com.qw.soul.permission.bean.Permission
import com.qw.soul.permission.bean.Permissions
import com.qw.soul.permission.callbcak.CheckRequestPermissionsListener
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    // 申请相机权限的requestCode
    private val PERMISSION_CAMERA_REQUEST_CODE = 0x00000001
    private var photoUri: Uri? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button.setOnClickListener { applyPermissionAndPhoto() }
    }

    private fun applyPermissionAndPhoto() {
        SoulPermission.getInstance()
            .checkAndRequestPermissions(
                Permissions.build(
                    Manifest.permission.CAMERA
                ),
                object : CheckRequestPermissionsListener {
                    override fun onAllPermissionOk(allPermissions: Array<out Permission>?) {
                        openCamera()
                    }

                    override fun onPermissionDenied(refusedPermissions: Array<out Permission>?) {
                    }
                })
    }

    /**
     * 调起照相机
     */
    private fun openCamera() {
        val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        //判断是否有相机
        if (captureIntent.resolveActivity(packageManager) != null) {
            photoUri = createImageUri()
            if (photoUri != null) {
                captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
                captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
                startActivityForResult(captureIntent, PERMISSION_CAMERA_REQUEST_CODE)
            }
        }
    }

    /**
     * 创建图片地址Uri，用于保存拍照后的照片
     */
    private fun createImageUri(): Uri? {
        val status = Environment.getExternalStorageState()
        //判断是否有SD卡，优先使用SD卡存储，当没有SD卡时使用手机储存
        return if (status.equals(Environment.MEDIA_MOUNTED)) {
            contentResolver.insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                ContentValues()
            )
        } else {
            contentResolver.insert(
                MediaStore.Images.Media.INTERNAL_CONTENT_URI,
                ContentValues()
            )
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        Glide.with(this).load(photoUri).into(imageView)
        if (requestCode == 1 && resultCode == RESULT_OK) {
            Glide.with(this).load(photoUri).into(imageView)
        }
        super.onActivityResult(requestCode, resultCode, data)
    }
}